package com.nd.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.*;
import com.nd.pojo.bean.*;
import com.nd.pojo.bo.*;
import com.nd.pojo.vo.*;
import com.nd.pojo.vo.wx.IndexUserOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.System;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : PengFei Zhang
 * @Data :2021/6/4  21:06
 * @Description :
 */
//用户管理  张鹏飞（Mr.Zhang）
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    //会员管理
    @Override
    public UserDataVo selectUserData(UserManagementBo userManagementBo) {
        //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(userManagementBo.getPage(), userManagementBo.getLimit());
        //UserExample  构造条件  可以是任意条件（重点是构造条件）  可以不断的and
        UserExample userExample = new UserExample();
        //每个service的方法都来创建example对象
        //排序信息
        userExample.setOrderByClause(userManagementBo.getSort() + " " + userManagementBo.getOrder());
        UserExample.Criteria criteria = userExample.createCriteria();
        if (userManagementBo.getUsername() != null && !"".equals(userManagementBo.getUsername())) {
            criteria.andUsernameLike("%" + userManagementBo.getUsername() + "%");
        }
        if (userManagementBo.getMobile() != null && !"".equals(userManagementBo.getMobile())) {
            criteria.andMobileLike("%" + userManagementBo.getMobile() + "%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        //可以获得分页信息
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        //获得所有条数信息
        long total = userPageInfo.getTotal();

        UserDataVo userData = new UserDataVo();
        userData.setTotal((int) total);
        userData.setItems(users);

        return userData;
    }

    //收货地址
    @Override
    public RecipentsAdressVo selectRecipientsAdress(RecipientsAdressBo recipientsAdressBo) {
        //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(recipientsAdressBo.getPage(), recipientsAdressBo.getLimit());
        // 构造条件  可以是任意条件（重点是构造条件）  可以不断的and
        AddressExample addressExample = new AddressExample();
        //排序信息
        addressExample.setOrderByClause(recipientsAdressBo.getSort() + " " + recipientsAdressBo.getOrder());
        //构造条件
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (recipientsAdressBo.getName() != null && !"".equals(recipientsAdressBo.getName())) {
            criteria.andNameLike("%" + recipientsAdressBo.getName() + "%");
        }
        if (recipientsAdressBo.getUserId() != null && !"".equals(recipientsAdressBo.getUserId())) {
            criteria.andUserIdEqualTo(recipientsAdressBo.getUserId());
        }
        //根据条件查询
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        //将查询的信息分页
        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);
        //得到信息总条数
        long total = addressPageInfo.getTotal();
        //构造响应所需
        List<RecipentAdressOfUserVo> recipentAdressOfUserVos = new ArrayList<>();
        for (Address address : addresses) {
            String area = addressMapper.selectRegionById(address.getAreaId());
            String city = addressMapper.selectRegionById(address.getCityId());
            String province = addressMapper.selectRegionById(address.getProvinceId());
            System.out.println(address.getIsDefault());
            RecipentAdressOfUserVo recipentAdressOfUserVo = new RecipentAdressOfUserVo(area, address.getIsDefault(),
                    address.getAreaId(), address.getAddress(), province, city, address.getName(), address.getMobile(),
                    address.getId(), address.getCityId(), address.getUserId(), address.getProvinceId());
            recipentAdressOfUserVos.add(recipentAdressOfUserVo);

        }

        RecipentsAdressVo recipentsAdressVo = new RecipentsAdressVo();
        recipentsAdressVo.setTotal((int) total);
        recipentsAdressVo.setItems(recipentAdressOfUserVos);

        return recipentsAdressVo;
    }

    //会员收藏
    @Override
    public UserCollectVo selectUserCollectBo(UserCollectBo userCollectBo) {
        //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(userCollectBo.getPage(), userCollectBo.getLimit());
        // 构造条件  可以是任意条件（重点是构造条件）  可以不断的and
        CollectExample collectExample = new CollectExample();
        //排序信息
        collectExample.setOrderByClause(userCollectBo.getSort() + " " + userCollectBo.getOrder());
        //构造条件
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userCollectBo.getUserId() != null && !"".equals(userCollectBo.getUserId())) {
            criteria.andUserIdEqualTo(userCollectBo.getUserId());
        }
        if (userCollectBo.getValueId() != null && !"".equals(userCollectBo.getValueId())) {
            criteria.andValueIdEqualTo(userCollectBo.getValueId());
        }
        //根据条件查询
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //将查询的信息分页
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        //得到信息总条数
        UserCollectVo userCollectVo = new UserCollectVo();
        long total = collectPageInfo.getTotal();
        userCollectVo.setTotal((int) total);
        userCollectVo.setItems(collects);
        return userCollectVo;
    }

    //会员足迹
    @Override
    public UserFootprintVo selectUserFootprint(UserFootprintBo userFootprintBo) {  //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(userFootprintBo.getPage(), userFootprintBo.getLimit());
        // 构造条件  可以是任意条件（重点是构造条件）  可以不断的and
        FootprintExample footprintExample = new FootprintExample();
        //排序信息
        footprintExample.setOrderByClause(userFootprintBo.getSort() + " " + userFootprintBo.getOrder());
        //构造条件
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userFootprintBo.getUserId() != null && !"".equals(userFootprintBo.getUserId())) {
            criteria.andUserIdEqualTo(userFootprintBo.getUserId());
        }
        if (userFootprintBo.getGoodsId() != null && !"".equals(userFootprintBo.getGoodsId())) {
            criteria.andGoodsIdEqualTo(userFootprintBo.getGoodsId());
        }
        //根据条件查询
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        //将查询的信息分页
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        UserFootprintVo userFootprintVo = new UserFootprintVo();
        //得到信息总条数
        long total = footprintPageInfo.getTotal();
        userFootprintVo.setTotal((int) total);
        userFootprintVo.setItems(footprints);


        return userFootprintVo;
    }

    //搜索历史
    @Override
    public UserHistoryListVo selectUserHistoryList(HistoryListBo historyListBo) {
        //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(historyListBo.getPage(), historyListBo.getLimit());
        // 构造条件  可以是任意条件（重点是构造条件）  可以不断的and
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        //排序信息
        searchHistoryExample.setOrderByClause(historyListBo.getSort() + " " + historyListBo.getOrder());
        //构造条件
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (historyListBo.getUserId() != null && !"".equals(historyListBo.getUserId())) {
            criteria.andUserIdEqualTo(historyListBo.getUserId());
        }
        if (historyListBo.getKeyword() != null && !"".equals(historyListBo.getKeyword())) {
            criteria.andKeywordLike("%" + historyListBo.getKeyword() + "%");
        }
        //综合查询
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        //获取分页
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        //获取所有条数信息
        long total = searchHistoryPageInfo.getTotal();
        UserHistoryListVo userHistoryListVo = new UserHistoryListVo();
        userHistoryListVo.setTotal((int) total);
        userHistoryListVo.setItems(searchHistories);
        return userHistoryListVo;
    }

    //意见反馈
    @Override
    public FeedbackListVo selectFeedbackList(FeedbackListBo feedbackListBo) {
        //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(feedbackListBo.getPage(), feedbackListBo.getLimit());
        // 构造条件  可以是任意条件（重点是构造条件）  可以不断的and
        FeedbackExample feedbackExample = new FeedbackExample();
        //排序信息
        feedbackExample.setOrderByClause(feedbackListBo.getSort() + " " + feedbackListBo.getOrder());
        //构造条件
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (feedbackListBo.getUsername() != null && !"".equals(feedbackListBo.getUsername())) {
            criteria.andUsernameLike("%" + feedbackListBo.getUsername() + "%");
        }
        if (feedbackListBo.getId() != null && !"".equals(feedbackListBo.getId())) {
            criteria.andIdEqualTo(feedbackListBo.getId());
        }
        //综合查询
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        //获取分页
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        //获取所有条数信息
        long total = feedbackPageInfo.getTotal();
        FeedbackListVo feedbackListVo = new FeedbackListVo();
        feedbackListVo.setTotal((int) total);
        feedbackListVo.setItems(feedbacks);
        return feedbackListVo;
    }

    //wx用户首页
    @Override
    public IndexUserOrderVo queryUserIndexVo(Integer id) {
        int unrecv = orderMapper.countByOrderStatus(id,401);
        int unpaid = orderMapper.countByOrderStatus(id,101);
        int unship = orderMapper.countByOrderStatus(id,201);
        int uncomment = 0;
        //根据订单id查询是否可以评论
        List<Order> orderList = orderMapper.selectOrderIdsByUserId(id);
        for (Order order : orderList) {
            if(order.getOrderStatus() == 401 || order.getOrderStatus() == 402){
                OrderGoods orderGoods = orderGoodsMapper.selectByOrderId(order.getId());
                if(orderGoods.getComment() == 0) uncomment++;
            }
        }
        return new IndexUserOrderVo(unrecv,uncomment,unpaid,unship);
    }
}
